using Microsoft.AspNetCore.Mvc;
using ConsoleApi.Services;
using ConsoleApi.Services.Interfaces;

namespace ConsoleApi.Controllers;

/// <summary>
/// 生产数据API控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class ProductionController : ControllerBase
{
    private readonly IProductionDataService _productionService;
    private readonly IWeatherAlertRuleEngine _ruleEngine;
    private readonly ILogger<ProductionController> _logger;

    public ProductionController(
        IProductionDataService productionService,
        IWeatherAlertRuleEngine ruleEngine,
        ILogger<ProductionController> logger)
    {
        _productionService = productionService;
        _ruleEngine = ruleEngine;
        _logger = logger;
    }

    /// <summary>
    /// 获取生产活动列表
    /// </summary>
    [HttpGet("activities/{activityType}")]
    public async Task<IActionResult> GetProductionActivities(string activityType)
    {
        try
        {
            var activities = await _productionService.GetProductionActivitiesAsync(activityType);
            return Ok(new
            {
                Success = true,
                Data = activities,
                Timestamp = DateTime.Now
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取生产活动失败");
            return BadRequest(new
            {
                Success = false,
                Error = ex.Message,
                Timestamp = DateTime.Now
            });
        }
    }

    /// <summary>
    /// 获取生产统计数据
    /// </summary>
    [HttpGet("statistics")]
    public async Task<IActionResult> GetProductionStatistics([FromQuery] string city, [FromQuery] DateTime date)
    {
        try
        {
            var statistics = await _productionService.GetProductionStatisticsAsync(city, date);
            return Ok(new
            {
                Success = true,
                Data = statistics,
                Timestamp = DateTime.Now
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取生产统计数据失败");
            return BadRequest(new
            {
                Success = false,
                Error = ex.Message,
                Timestamp = DateTime.Now
            });
        }
    }

    /// <summary>
    /// 更新生产活动状态
    /// </summary>
    [HttpPut("activities/{activityId}/status")]
    public async Task<IActionResult> UpdateActivityStatus(string activityId, [FromBody] string status)
    {
        try
        {
            var result = await _productionService.UpdateActivityStatusAsync(activityId, status);
            return Ok(new
            {
                Success = result,
                Message = result ? "状态更新成功" : "状态更新失败",
                Timestamp = DateTime.Now
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "更新生产活动状态失败");
            return BadRequest(new
            {
                Success = false,
                Error = ex.Message,
                Timestamp = DateTime.Now
            });
        }
    }

    /// <summary>
    /// 获取天气影响分析
    /// </summary>
    [HttpGet("weather-impact")]
    public async Task<IActionResult> GetWeatherImpact([FromQuery] string city, [FromQuery] string activityType = "户外作业")
    {
        try
        {
            var analysis = await _ruleEngine.AnalyzeProductionImpactAsync(city, activityType);
            return Ok(new
            {
                Success = true,
                Data = analysis,
                Timestamp = DateTime.Now
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "获取天气影响分析失败");
            return BadRequest(new
            {
                Success = false,
                Error = ex.Message,
                Timestamp = DateTime.Now
            });
        }
    }
} 